﻿using EleCho.GoCqHttpSdk;
using Microsoft.Extensions.Logging;
using PluginSdk;

namespace AdminPlugin;
public class Admin(List<long> Admins, Action SaveConfig) : IPlugin
{
    public string Name => "管理插件";

    public string Description => "添加或删除管理员";

    public ICommandBase[] Commands { get; set; } = [];

    public ILogger Logger { get; set; }

    List<long> Admins = Admins;

    Action OnAndminChanged = SaveConfig;

    public void Config()
    {
        Commands = [new AddAdminCommand(Admins, OnAndminChanged)];
    }

    public void ConfigLogger(ILoggerFactory loggerFactory)
    {
        Logger = loggerFactory.CreateLogger<Admin>();
    }

    public void ConfigSession(ICqActionSession session)
    {
    }
}

public class AddAdminCommand(List<long> Admins, Action SaveConfig) : ITextCommand
{
    public string Match => "^#设置管理";

    public Permission Permission => Permission.SameUser;

    public string Name => "设置管理";

    public string Description => "将用户添加到管理员列表";

    public async Task Handler(IMessageStream stream)
    {
        var message = await stream.ReadMessage();
        if(Admins.Contains(message.UserId))
        {
            await stream.WriteMessage(new("您已经是管理员了"));
            return;
        }
        var capcha = Convert.ToBase64String(BitConverter.GetBytes(Random.Shared.NextInt64()));
        Console.WriteLine(capcha);
        await stream.WriteMessage(new("请输入验证码："));
        var capchaInput = (await stream.ReadMessage()).Message.Text.Trim();
        if(capchaInput == capcha)
        {
            Admins.Add(message.UserId);
            SaveConfig();
            await stream.WriteMessage(new("已设置管理员"));
            return;
        }
        else
        {
            await stream.WriteMessage(new("验证码错误！"));
            return;
        }
    }
}